package com.mnsq.book.service.impl;

import com.mnsq.book.mapper.BookMapper;
import com.mnsq.book.service.BookService;
import com.mnsq.common.bean.annotation.AutoCache;
import com.mnsq.common.bean.condition.CommonBaseCondition;
import com.mnsq.common.bean.conf.ThreadLocalUserId;
import com.mnsq.common.bean.dto.PageInfo;
import com.mnsq.common.bean.dto.UsersBookDto;
import com.mnsq.common.bean.entity.book.Book;
import com.mnsq.common.bean.entity.book.UsersBook;
import com.mnsq.common.bean.entity.book.UsersBookList;
import com.mnsq.common.bean.enums.StatusEnum;
import com.mnsq.common.bean.vo.book.BookCondition;
import com.mnsq.common.dao.UsersBookListRepository;
import com.mnsq.common.dao.UsersBookRepository;
import com.mnsq.common.tools.jpa.JpaExample;
import com.mnsq.common.tools.tool.BeanCopyUtils;
import lombok.extern.slf4j.Slf4j;
import matrix.module.common.bean.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @date 2019/1/21 0021
 */
@Slf4j
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private UsersBookRepository usersBookRepository;

    @Autowired
    private UsersBookListRepository usersBookListRepository;

    @Override
    public PageInfo<Book> queryPageByCondition(BookCondition bc) {
        PageInfo<Book> info = new PageInfo<>();
        info.setTotalSize(bookMapper.queryTotal(bc));
        info.setContent(bookMapper.queryPage(bc));
        return info;
    }

    @Override
    @AutoCache(cacheType = AutoCache.CacheType.HOURS, value = 12L, cls = Book.class, prefix = "bookTopOne")
    public Book topFive(BookCondition bookCondition) {
        // LTS computer the top five
        // TODO zjdo get the top five
        return null;
    }

    @Override
    public Result read(BookCondition bookCondition) {
        Long userId = ThreadLocalUserId.CURRENT_USER_ID.get();
        bookCondition.setUserId(userId);
        Optional<UsersBook> one = usersBookRepository.findOne(JpaExample.getInstance().getExample(bookCondition, new UsersBook()));
        UsersBook ub = null;
        if (one.isPresent()) {
            ub = one.get();
            ub.setUpdateTime(new Date());
            ub.setScore(BigDecimal.valueOf(Double.valueOf(bookCondition.getScore())).setScale(2, BigDecimal.ROUND_HALF_UP));
            ub.setComment(bookCondition.getComment());
        } else {
            ub.createInit();
            BeanCopyUtils.copyNullProperties(bookCondition, ub);
            //  取消书单列表内的记录
            UsersBook ubook = usersBookListRepository.findByUserIdAndAndBookIdAndStatus(userId, bookCondition.getBookId(), StatusEnum.STATUS_VALID.value);
            if (ubook != null) {
                usersBookRepository.delete(ubook);
            }
        }
        usersBookRepository.saveAndFlush(ub);
        return Result.success(null);
    }

    @Override
    public Result guessUserLike() {
        // todo zjdo guessUserLike
        return null;
    }

    @Override
    public Result userReadPage(CommonBaseCondition baseCondition) {
        // todo zjdo pageList
        List<UsersBookDto> usersBookDtos = bookMapper.userReadPage(baseCondition);
        return Result.success(usersBookDtos);
    }

    @Override
    public Result userReadDetail(BookCondition bookCondition) {
        // TODO book detail
        return Result.success(null);
    }

    @Override
    public Result addBookList(BookCondition bookCondition) {
        // TODO add book list
        return null;
    }

    @Override
    public Result addBookListPage(BookCondition bookCondition) {
        List<UsersBookList> all = usersBookListRepository.findAll(JpaExample.getInstance().getExample(bookCondition, new UsersBookList()));
        List<Long> bookIds = all.parallelStream().map(p -> p.getBookId()).collect(Collectors.toList());
        List<UsersBookDto> usersBooks = bookMapper.findBookListPage(bookIds, bookCondition);
        return Result.success(usersBooks);
    }
}
